package com.wsz.eduservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wsz.commonutils.R;
import com.wsz.eduservice.pojo.Resource;
import com.wsz.eduservice.service.ResourceService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "小节管理")
@RequestMapping("/eduservice/resource")
@RestController
public class EudResourceController {
    @Autowired
    private ResourceService resourceService;

    @PostMapping("/save")
    public R insert(@RequestBody Resource resource){
        boolean save = resourceService.save(resource);
        if(save) return R.ok();
        else return R.error().message("上传失败");
    }

    @PostMapping("/update")
    public R update(@RequestBody Resource resource){
        System.out.println(resource);
        boolean update = resourceService.updateById(resource);
        if(update) return R.ok();
        else return R.error().message("修改失败");
    }

    @GetMapping("/getByCourseId/{id}")
    public R getByCourseId(@PathVariable String id){
        List<String> url = new ArrayList<>();
        QueryWrapper<Resource> queryWrapper = new QueryWrapper();
        queryWrapper.select("url");
        queryWrapper.eq("course_id",id);
        List<Resource> list = resourceService.list(queryWrapper);

        for (int i = 0; i < list.size(); i++) {
            Resource resource = list.get(i);
            url.add(resource.getUrl());
        }
        return R.ok().data("urls",url);
    }

}
